/*!
    \file  gd32e10x_it.c
    \brief interrupt service routines
    
    \version 2018-03-26, V1.0.0, demo for GD32E103
*/

/*
    Copyright (c) 2018, GigaDevice Semiconductor Inc.

    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32e10x_it.h"
#include "systick.h"
#include "sleep.h"
#include "L610.h"


/*!
    \brief      this function handles NMI exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void NMI_Handler(void)
{
}

/*!
    \brief      this function handles HardFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void HardFault_Handler(void)
{
    /* if Hard Fault exception occurs, go to infinite loop */
    while (1){
    }
}

/*!
    \brief      this function handles MemManage exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void MemManage_Handler(void)
{
    /* if Memory Manage exception occurs, go to infinite loop */
    while (1){
    }
}

/*!
    \brief      this function handles BusFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void BusFault_Handler(void)
{
    /* if Bus Fault exception occurs, go to infinite loop */
    while (1){
    }
}

/*!
    \brief      this function handles UsageFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void UsageFault_Handler(void)
{
    /* if Usage Fault exception occurs, go to infinite loop */
    while (1){
    }
}

/*!
    \brief      this function handles SVC exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void SVC_Handler(void)
{
}

/*!
    \brief      this function handles DebugMon exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DebugMon_Handler(void)
{
}

/*!
    \brief      this function handles PendSV exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void PendSV_Handler(void)
{
}

/*!
    \brief      this function handles SysTick exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void SysTick_Handler(void)
{
	//delay_decrement();
}

/*!
    \brief      this function handles external lines 0 interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/
void EXTI0_IRQHandler(void)
{
}


/*!
    \brief      this function handles USART RBNE interrupt request and TBE interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART0_IRQHandler(void)
{
	__IO uint8_t da;
	
	#if _EnSleep_def
	wakeup();
	#endif
	

	if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
	{
		da = usart_data_receive(USART0);     
		//usart_interrupt_disable(USART0, USART_INT_RBNE);
				
		
		//接收L610芯片串口中断数据

		
	}

	if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE))
	{
		/* transmit data */
		usart_interrupt_disable(USART0, USART_INT_TBE);
	}
	
}

void USART1_IRQHandler(void)
{
	__IO uint8_t da;
	
	#if _EnSleep_def
	wakeup();
	#endif
	

	if(RESET != usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE))
	{
		da = usart_data_receive(USART1);     
		//usart_interrupt_disable(USART0, USART_INT_RBNE);
				
		//dma不会进入中断
		
	}

	if(RESET != usart_interrupt_flag_get(USART1, USART_INT_FLAG_TBE))
	{
		/* transmit data */
		usart_interrupt_disable(USART1, USART_INT_TBE);
	}
}

void USART2_IRQHandler(void)
{
	__IO uint8_t da;
	
	#if _EnSleep_def
	wakeup();
	#endif
	

	if(RESET != usart_interrupt_flag_get(USART2, USART_INT_FLAG_RBNE))
	{
		da = usart_data_receive(USART2);     
		//usart_interrupt_disable(USART2, USART_INT_RBNE);		
		
		//接收L610芯片串口中断数据
	}

	if(RESET != usart_interrupt_flag_get(USART2, USART_INT_FLAG_TBE))
	{
		/* transmit data */
		usart_interrupt_disable(USART2, USART_INT_TBE);
	}
	
}



/*
用于唤醒休眠的中断
*/
void TIMER1_IRQHandler(void)
{
	
	
	if(SET == timer_interrupt_flag_get(TIMER1, TIMER_INT_UP))
	{
		/* clear channel 0 interrupt bit */
		timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);

	}
}

/*
用于提供毫秒定时的中断, 实际不会进入，增加代码只是预防
*/
void TIMER0_UP_TIMER9_IRQHandler(void)
{
	if(SET == timer_interrupt_flag_get(TIMER0, TIMER_INT_UP))
	{
		timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_UP);
	}
}

extern uint8_t RF_Recevie_Code(uint8_t curbit);
extern uint8_t	sample_io(void);

extern void led_on(void);
extern void led_off(void);

u32  cnt2 = 0;
u32  flip2 = 0;


void TIMER2_IRQHandler(void)
{
	u8 sample_bit = 0;

	if(SET == timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP))
	{
		timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
		
		sample_bit = sample_io();
		sample_bit ^=0x01;	//取反
		
		RF_Recevie_Code(sample_bit);
		
		#if 0		/* 使用发光二极管看定时器时间是否准确 */
			{
				cnt2++;
				if (cnt2 > (1000000/68))
				{
					cnt2 = 0;
					if (flip2 == 0)
					{
						flip2 = 1;
						led_on();
					}
					else
					{
						flip2 = 0;
						led_off();
					}
				}
			}
		#endif
			
	}
}

